﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace MyMusicPlay._4._0
{
    public class LongClicks<T> where T : FrameworkElement
    {
        private static Dictionary<int?, object> _longCLicks = new();

        public static void Create(T element, Action<object, MouseEventArgs> action)
         => new LongClicks<T>(element) { _moveAction = action };
        public static void Create(T element, Action<object, MouseEventArgs> moveAction, Action<T> action)
           => new LongClicks<T>(element) { _moveAction = moveAction, _action = action };



        private DateTime? _clickTime;
        private T _element;
        private Task _task;
        private bool? _taskPersistent;
        private bool? _IsMove;
        private Action<object, MouseEventArgs> _moveAction;
        private Action<T> _action;
        private int? Id;
        private static int index = 0;

        private LongClicks(T element)
        {
            Id = index++;
            _element = element;
            _clickTime = DateTime.Now;
            _taskPersistent = true;
            _IsMove = false;
            _element.MouseMove += MouseMove;
            _element.MouseUp += MouseUp;
            _element.MouseLeave += MouseLeave;

            _task = Task.Run(() =>
            {
                while (_taskPersistent ?? false)
                {
                    if (_clickTime is not null && _clickTime.Value.AddMilliseconds(1000) < DateTime.Now)
                    {
                        _IsMove = true;
                        element.Dispatcher.Invoke(() =>
                        {
                            _action?.Invoke(element);
                        });
                    }
                    Task.Delay(200).Wait();
                }
            });

            _longCLicks.Add(Id, this);
        }

        private void MouseDown(object sender, MouseButtonEventArgs e)
        {

        }
        private void MouseMove(object sender, MouseEventArgs e)
        {
            if (_IsMove ?? false)
            {
                _moveAction.Invoke(sender, e);
            }
            else
            {
                _clickTime = DateTime.Now;
            }
        }
        private void MouseUp(object sender, MouseButtonEventArgs e)
        {
            _taskPersistent = false;
            Dispose();
        }
        private void MouseLeave(object sender, MouseEventArgs e)
        {
            _taskPersistent = false;
            Dispose();
        }

        private void Dispose()
        {
            if (_element is not null)
            {
                _element.MouseMove -= MouseMove;
                _element.MouseUp -= MouseUp;
                _element.MouseLeave -= MouseLeave;
            }
            _element = null;
            _clickTime = null;
            _taskPersistent = null;
            _longCLicks.Remove(Id);
            Id = null;
        }

    }
}
